
static char RCSId[]="$Id: Preferences.m,v 1.1.1.1 1993/03/18 03:35:22 davis Exp $";


#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/Listener.h>
#import <appkit/Matrix.h>
#import <appkit/Panel.h>

#import <defaults/defaults.h>

#import "Gnuplot.h"
#import "Preferences.h"

#define	OWNER	"Gnuplot"		/* Owner of defaults	*/

static NXDefaultsVector GnuplotDefaults = {
    {"ConstantUpdate", "1"},		/* Update after every change	*/
    {"NewDocument", "1"},		/* Create a new doc at launch	*/
    {"DefaultInspector", "0"},		/* Default Pane is General	*/
    {"ReadInitFile", "1"},		/* Currently unused		*/
    {"InitFile", "~/.gnuplot"},		/* Currently unused		*/
    {"HalvePlot", "1"},			/* Make plots half-size		*/
    {NULL}
};


@implementation Preferences

- init
{
    [super init];
    NXRegisterDefaults(OWNER, GnuplotDefaults);

    window = nil;

    /*  Set instance variables to their defaults.  */

    constantUpdate = *NXGetDefaultValue(OWNER, "ConstantUpdate") - '0';
    [Gnuplot setConstantUpdate:constantUpdate];

    newDocument = (*NXGetDefaultValue(OWNER, "NewDocument") != '0');
    defaultInspector = *NXGetDefaultValue(OWNER, "DefaultInspector") - '0';

    halvePlot = (*NXGetDefaultValue(OWNER, "HalvePlot") != '0');
    [Gnuplot setHalvePlot:halvePlot];

    isInitFile = (*NXGetDefaultValue(OWNER, "ReadInitFile") != '0');
    initFile = NXCopyStringBufferFromZone(NXGetDefaultValue(OWNER, "InitFile"),
					  [self zone]);
    return self;
}


- free
{
    return [super free];
}


- awakeFromNib
{
    [window setFrameUsingName:"PreferencesPanel"];
    [window setFrameAutosaveName:"PreferencesPanel"];

    [constantUpdateMatrix selectCellWithTag:constantUpdate];

    [newDocumentButton setState:newDocument];
    [defaultInspectorMatrix selectCellWithTag:defaultInspector];
    [halvePlotMatrix selectCellWithTag:halvePlot? 1:0];

    /*  
     *  The services "preferences" are not actually stored in the 
     *  defaults database.  We check to see if the services are 
     *  enabled and set the controls based on that.
     */
    [addDataToCurrentButton setState:
	     NXIsServicesMenuItemEnabled ("Gnuplot/Add Data to Current Plot")];

    [addDataToNewButton setState:
	     NXIsServicesMenuItemEnabled ("Gnuplot/Plot Data in New Plot")];

//    [initFileButton setState:isInitFile];
//    [initFileField setStringValue:initFile];

    return self;
}


- showPanel:sender
{
    if (!window) {
	[NXApp loadNibSection: "Preferences.nib" 
			owner: self
		    withNames: NO
		     fromZone: [self zone]];
    }

    [window makeKeyAndOrderFront:self];
    return self;
}



- doSetConstantUpdate:sender
{
    char aString[2];

    constantUpdate = [constantUpdateMatrix selectedTag];
    aString[0] = '0' + constantUpdate;
    aString[1] = '\0';
    NXWriteDefault (OWNER, "ConstantUpdate", aString);

    [Gnuplot setConstantUpdate:constantUpdate];
    return self;
}



- (int)constantUpdate
{
    return constantUpdate;
}


- doSetNewDocument:sender
{
    const char *aString;

    if (newDocument = [newDocumentButton state]) aString = "1";
    else aString = "0";
    NXWriteDefault (OWNER, "NewDocument", aString);

    return self;
}



- (BOOL) newDocument
{
    return newDocument;
}


- doSetDefaultInspector:sender
{
    char aString[2];

    defaultInspector = [defaultInspectorMatrix selectedTag];
    aString[0] = '0' + defaultInspector;
    aString[1] = '\0';
    NXWriteDefault (OWNER, "DefaultInspector", aString);
    return self;
}


- (int)defaultInspector
{
    return defaultInspector;
}


- doSetHalvePlot:sender
{
    const char *aString;

    if (halvePlot = [halvePlotMatrix selectedTag]) aString = "1";
    else aString = "0";
    NXWriteDefault (OWNER, "HalvePlot", aString);

    [Gnuplot setHalvePlot:halvePlot];

    return self;
}


- (BOOL)halvePlot
{
    return halvePlot;
}



- doSetAddDataToCurrent:sender
{
    NXSetServicesMenuItemEnabled ("Gnuplot/Add Data to Current Plot",
				  [addDataToCurrentButton state]);
    [addDataToCurrentButton setState:NXIsServicesMenuItemEnabled
				     ("Gnuplot/Add Data to Current Plot")];
    return self;
}



- (BOOL)addDataToCurrent
{
    return NXIsServicesMenuItemEnabled ("Gnuplot/Add Data to Current Plot");
}



- doSetAddDataToNew:sender
{
    NXSetServicesMenuItemEnabled ("Gnuplot/Plot Data in New Plot",
				  [addDataToNewButton state]);
    [addDataToNewButton setState:NXIsServicesMenuItemEnabled
				 ("Gnuplot/Plot Data in New Plot")];
    return self;
}



- (BOOL)addDataToNew
{
    return NXIsServicesMenuItemEnabled ("Gnuplot/Plot Data in New Plot");
}



- doSetIsInitFile:sender
{
    const char *aString;

    if (isInitFile = [initFileButton state]) aString = "1";
    else aString = "0";
    NXWriteDefault (OWNER, "ReadInitFile", aString);

    return self;
}


- doSetInitFileWithOpenPanel:sender
{
    return self;
}



- (BOOL) isInitFile
{
    return isInitFile;
}


- doSetInitFile:sender
{
    NXWriteDefault (OWNER, "ReadInitFile", [initFileField stringValue]);
    return self;
}



- (const char *) initFile
{
    return initFile;
}


// Shuts up the compiler about unused RCSId
- (const char *) rcsid
{
    return RCSId;
}


@end
